﻿#include "OneWayLinkList.h"

//1.链表节点数据类型；
struct OneWayLinkNode
{
	void* data;
	struct OneWayLinkNode* next;
};

//2.链表的数据类型；
struct OneWayLinkList
{
	//3.头节点；
	struct OneWayLinkNode header;
	int size;
};

//5.初始化链表；
LinkList Init_OneWayLinkList()
{
	struct OneWayLinkList* list = malloc(sizeof(struct OneWayLinkList));
	if (NULL == list)
	{
		return NULL;
	}
	list->header.data = NULL;
	list->header.next = NULL;
	list->size = 0;

	return list;
}
//6.插入节点，按位置插入；
void Insert_OneWayLinkList(LinkList list, int pos, void* data)
{
	if (NULL == list)
	{
		return;
	}
	if (NULL == data)
	{
		return;
	}

	struct OneWayLinkList* myList = (struct OneWayLinkList*)list;
	if (pos<0 || pos>myList->size)
	{
		pos = myList->size;
	}

	//9.pCurrent指向头节点；
	struct OneWayLinkNode* pCurrent = &(myList->header);
	for (int i = 0; i < pos; ++i)
	{
		//11.pCurrent指向pos的位置；
		pCurrent = pCurrent->next;
	}
	//10.创建新节点；
	struct OneWayLinkNode* newNode = malloc(sizeof(struct OneWayLinkNode));
	newNode->data = data;
	newNode->next = NULL;

	//12.新节点插入到链表中；插入链表时，pos是索引，比如可以插入到0处；pCurrent指向pos的前一个节点；
	newNode->next = pCurrent->next;
	pCurrent->next = newNode;

	myList->size++;

}
//7.遍历链表；
Foreach_OneWayLinkList(LinkList list, FOREACH myForeach)
{
	if (NULL == list)
	{
		return;
	}
	if (NULL == myForeach)
	{
		return;
	}
	struct OneWayLinkList* myList = (struct OneWayLinkList*)list;
	//13.pCurrent直接指向第一个节点，头结点header在链表中没有意义；
	struct OneWayLinkNode* pCurrent = myList->header.next;
	while (pCurrent != NULL)
	{
		//14.自定义回调函数myForeach;
		myForeach(pCurrent->data);
		pCurrent = pCurrent->next;
	}
}

//19.按位置删除链表中的元素；
void RemoveByPos_OneWayLinkList(LinkList list, int pos)
{
	if (NULL == list)
	{
		return;
	}
	struct OneWayLinkList* myList = (struct OneWayLinkList*)list;
	if (pos<0 || pos>myList->size - 1)
	{
		return;
	}
	struct OneWayLinkNode* pCurrent = &(myList->header);
	//30.找到要删除元素节点的前一位节点位置；
	for (int i = 0; i < pos; ++i)
	{
		pCurrent = pCurrent->next;
	}

	//31.先保存待删除的元素节点，pos位置；
	struct OneWayLinkNode* pDel = pCurrent->next;
	//32.重新建立待删除节点的前驱和后继的节点关系；
	pCurrent->next = pDel->next;
	//33.释放删除节点内存；
	free(pDel);
	pDel = NULL;

	myList->size--;
}
//20.按值删除链表中的元素节点；
void RemoveByVal_OneWayLinkList(LinkList list, void* data, COMPARE compare)
{
	if (NULL == list)
	{
		return;
	}
	if (NULL == data)
	{
		return;
	}
	if (NULL == compare)
	{
		return;
	}

	struct OneWayLinkList* myList = (struct OneWayLinkList*)list;
	//34.辅助指针变量；
	struct OneWayLinkNode* pPrev = &(myList->header);
	struct OneWayLinkNode* pCurrent = pPrev->next;

	while (pCurrent != NULL)
	{
		if (compare(pCurrent->data, data))
		{
			//35.找到了待删除元素节点的位置；
			pPrev->next = pCurrent->next;
			//36.释放删除节点内存；
			free(pCurrent);
			pCurrent = NULL;
			myList->size--;
			break;
		}
		pPrev = pCurrent;
		pCurrent = pCurrent->next;
	}
}
//21.清空链表；
void Clear_OneWayLinkList(LinkList list)
{
	if (NULL == list)
	{
		return;
	}
	struct OneWayLinkList* myList = (struct OneWayLinkList*)list;
	//25.辅助指针变量pCurrent；
	struct OneWayLinkNode* pCurrent = myList->header.next;
	while (pCurrent != NULL)
	{
		//26.先缓存下一个节点的地址；
		struct OneWayLinkNode* pNext = pCurrent->next;
		//27.释放当前节点内存；
		free(pCurrent);

		pCurrent = pNext;
	}
	//28.清空链表，就是将链表中的元素全部销毁；所有节点全部销毁；
	myList->header.next = NULL;
	myList->size = 0;
}
//22.获取链表的大小；
int Size_OneWayLinkList(LinkList list)
{
	if (NULL == list)
	{
		return -1;
	}
	struct OneWayLinkList* myList = (struct OneWayLinkList*)list;
	return myList->size;
}
//24.销毁链表；
void Destroy_OneWayLinkList(LinkList list)
{
	if (NULL == list)
	{
		return;
	}
	Clear_OneWayLinkList(list);
	//29.销毁链表，就是将链表中的元素全部销毁，所有节点全部销毁，同时销毁链表；
	free(list);
	list = NULL;
}